package com.ycsj.goldmedalnewconcept.common.activity;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.just.agentweb.AgentWeb;
import com.just.agentweb.WebChromeClient;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.goldmedalnewconcept.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 通用的加载网址的WebView界面
 * Created by Administrator on 2019/5/9 0009.
 */

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.rl_content)
    RelativeLayout rlContent;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    private AgentWeb mAgentWeb;

    //传递的URL地址
    private String url;

    /**
     * 跳转到当前界面的方法
     *
     * @param context
     * @param url
     */
    public static void goActivity(Context context, String url) {
        Intent it = new Intent(context, WebViewActivity.class);
        it.putExtra("url", url);
        context.startActivity(it);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_webview;
    }

    @Override
    protected void init() {

        initStateBar();

        //初始化标题
        initTitle();

        //初始化配置
        initAndAddWebView();
    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        url = getIntent().getStringExtra("url");
    }

    /**
     * 初始化WebView并添加到视图
     */
    private void initAndAddWebView() {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(rlContent, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .setWebChromeClient(mWebChromeClient)
                .createAgentWeb()
                .ready()
                .go(url);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @OnClick(R.id.iv_back)
    public void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
        }
    }

    /**
     * 设置监听
     */
    protected com.just.agentweb.WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            //  super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (tvTitle != null && !TextUtils.isEmpty(title)) {
                if (title.length() > 10) {
                    title = title.substring(0, 8).concat("...");
                }
            }
            tvTitle.setText(title);
        }
    };
}
